package com.example.printservice.utils.impl;

import com.aspose.cells.License;
import com.aspose.cells.Workbook;
import com.aspose.words.Document;
import com.aspose.words.SaveFormat;
import com.example.printservice.constants.Constants;
import com.example.printservice.utils.ToPDF;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

@Slf4j
@Component
public class ToPDFImpl implements ToPDF {
    // 返回空，说明文件无法解析
    @Override
    public String toPdf(String path) {
        System.out.println(path);
        Path filePath = FileSystems.getDefault().getPath(path);
        System.out.println(filePath);
        String fileName = filePath.getFileName().toString();
        String pdfName = fileName.substring(0, fileName.lastIndexOf(".")) + ".pdf";
        String resultPath = null;
        if (fileName.endsWith(".pdf")) {
            fileName = pdfName;
            resultPath = pdfToWord(filePath.toString(), Constants.FILE_PATH + fileName.substring(0, fileName.lastIndexOf(".")) + ".docx");
            resultPath = toPdf(resultPath);
        } else if (fileName.endsWith(".doc") || fileName.endsWith(".docx")) {
            fileName = pdfName;
            resultPath = wordToPdf(filePath.toString(), Constants.RESULT_PATH + fileName);
        } else if (fileName.endsWith(".xlsx") || fileName.endsWith(".xls")) {
            fileName = pdfName;
            resultPath = excelToPdf(filePath.toString(), Constants.RESULT_PATH + fileName);
        } else {
            try {
                System.out.println(fileName);
                throw new Exception("无法解析的文件");
            } catch (Exception e) {
                log.error("无法解析的文件");
            }
        }
        return resultPath;
    }

    @Override
    public String wordToPdf(String filePath, String resultPath) {
        try {
            ClassLoader classLoader = this.getClass().getClassLoader();
            com.aspose.words.License license = new com.aspose.words.License();
            license.setLicense(classLoader.getResourceAsStream("license.xml"));
            long old = System.currentTimeMillis();
            FileOutputStream os = new FileOutputStream(resultPath);
            Document doc = new Document(filePath);
            doc.save(os, SaveFormat.PDF);
            os.close();
            long now = System.currentTimeMillis();
            System.out.println("共耗时：" + ((now - old) / 1000.0) + "秒");  //转化用时
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultPath;
    }


    @Override
    public String picToPdf(String fileName, String resultPath) {
        return "";
    }

    @Override
    public String excelToPdf(String fileName, String resultPath) {
        try {
            ClassLoader classLoader = this.getClass().getClassLoader();
            com.aspose.cells.License license = new com.aspose.cells.License();
            license.setLicense(classLoader.getResourceAsStream("license.xml"));
            long old = System.currentTimeMillis();
            FileOutputStream os = new FileOutputStream(resultPath);
            Workbook excel = new Workbook(fileName);//加载源文件数据
            excel.save(os, com.aspose.cells.SaveFormat.PDF);//设置转换文件类型并转换
            os.close();
            long now = System.currentTimeMillis();
            System.out.println("共耗时：" + ((now - old) / 1000.0) + "秒");  //转化用时
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultPath;
    }

    @Override
    public String pdfToWord(String filePath, String resultPath) {
        try {
            long old = System.currentTimeMillis();
            FileOutputStream os = new FileOutputStream(resultPath);
            com.aspose.pdf.Document doc = new com.aspose.pdf.Document(filePath);//加载源文件数据
            doc.save(os, com.aspose.pdf.SaveFormat.DocX);//设置转换文件类型并转换
            os.close();
            long now = System.currentTimeMillis();
            System.out.println("共耗时：" + ((now - old) / 1000.0) + "秒");  //转化用时
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultPath;
    }
}
